﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Tsanie.OsuDevice.Models;
using System.IO;
using Tsanie.OsuDevice.Components;

namespace Tsanie.OsuDevice.Utils {
    class BeatmapUtil {
        private static int _localReaded;
        //private static int _webReaded;
        private static StreamWriter _writer = null;
        private static object _sync = new object();

        public static ListViewItem CreateListViewItem(Beatmap bm) {
            ListViewItem lvi = new ListViewItem(new string[] {
                bm.Title,
                bm.Artist,
                bm.Creator,
                bm.Date,
                ""
            }, "updating");
            lvi.Tag = bm;
            return lvi;
        }

        public static void ResetLocalReaded() {
            _localReaded = 0;
            if (_writer != null) {
                try {
                    _writer.Flush();
                    _writer.Close();
                    _writer.Dispose();
                } catch { }
                _writer = null;
            }
        }
        //public static void ResetWebReaded() { _webReaded = 0; }
        public static void ReadBeatmapInfo(ListViewItem item, DirectoryInfo song, MainForm form, bool append) {
            lock (_sync) {
                try {
                    if (_writer == null)
                        _writer = new StreamWriter(Config.c_AppPath + "\\beatmaps.txt", append, Encoding.UTF8);
                    Beatmap bm = (Beatmap)item.Tag;
                    List<string> versions = new List<string>();
                    foreach (FileInfo fi in song.GetFiles("*.osu", SearchOption.TopDirectoryOnly)) {
                        StreamReader reader = new StreamReader(fi.OpenRead(), true);
                        string line;
                        while ((line = reader.ReadLine()) != null) {
                            if (line.StartsWith("Title:")) {
                                bm.Title = line.Substring(6).Trim();
                            } else if (line.StartsWith("Artist:")) {
                                bm.Artist = line.Substring(7).Trim();
                            } else if (line.StartsWith("Creator:")) {
                                bm.Creator = line.Substring(8).Trim();
                            } else if (line.StartsWith("Version:")) {
                                versions.Add(line.Substring(8).Trim());
                            } else if (line.StartsWith("Source:")) {
                                bm.Source = line.Substring(7).Trim();
                            } else if (line.StartsWith("Tags:")) {
                                bm.Tags = line.Substring(5).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            }
                            // 如果信息读取完毕跳出循环
                            if (line == "[Difficulty]")
                                break;
                        }
                        reader.Close();
                    }
                    bm.Versions = versions.ToArray();
                    Util.ThreadSafeRun(item.ListView, delegate {
                        item.SubItems[0].Text = bm.Title;
                        item.SubItems[1].Text = bm.Artist;
                        item.SubItems[2].Text = bm.Creator;
                        if (append)
                            item.SubItems[3].Text = bm.Date;
                        _writer.WriteLine(bm.ToString());
                    });
                    _localReaded++;
                    if (_localReaded >= item.ListView.Items.Count) {
                        try {
                            _writer.Flush();
                            _writer.Close();
                            _writer.Dispose();
                        } catch { }
                        _writer = null;
                        Util.ThreadSafeRun(form, delegate { form.EnabledRefresh(true); });
                        form.isRunning = false;
                    }
                } catch (Exception ex) {
                    LogUtil.Error(ex, null);
                }
            }
        }
    }
}
